package com.sprocomm.agingtest.Video;

import com.sprocomm.agingtest.BaseActivity;
import com.sprocomm.agingtest.Config;
import com.sprocomm.agingtest.R;
import com.sprocomm.agingtest.views.CDTTextView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class VideoActivity extends BaseActivity implements CDTTextView.OnCountDownTimerCallBack, MyConnChangeReceiver.OnNetChange {
    private CDTTextView mCDTTextView;
    private JCVideoPlayerStandard jCVideoPlayer;
    private MyConnChangeReceiver myReceiver;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        mCDTTextView = (CDTTextView) findViewById(R.id.timer);
        mCDTTextView.setCountDownTimer(Config.VIDEO_PLAYER_TEST_TIME, 1000);
        mCDTTextView.setTimerStart();
        mCDTTextView.setOnCountDownTimerCallBack(this);
//http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4
        jCVideoPlayer = (JCVideoPlayerStandard) findViewById(R.id.videocontroller1);
        jCVideoPlayer.setUp("http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4",
                JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "Test Video");
        jCVideoPlayer.loop = true;
        jCVideoPlayer.thumbImageView.setImageResource(R.mipmap.ic_launcher);

        jCVideoPlayer.startButton.performClick();
        registerReceiver();
   }

    @Override
    protected void onDestroy() {
        JCVideoPlayer.releaseAllVideos();
        if (myReceiver != null){
            this.unregisterReceiver(myReceiver);
        }
        super.onDestroy();
    }

    @Override
    public void setOnTick(long millisUntilFinished) {

    }

    @Override
    public void setOnFinish() {
        JCVideoPlayer.releaseAllVideos();
        setPassBtnEnable(true);
    }

    public class ConnChangeReciver extends BroadcastReceiver {

        public ConnChangeReciver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {

        }
    }

    private  void registerReceiver(){
        IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        myReceiver=new MyConnChangeReceiver(this);
        this.registerReceiver(myReceiver, filter);
    }
    //网络改变
    @Override
    public void onNetChange(int newState) {
        if (newState != ConnectivityManager.TYPE_WIFI) {
            //fail
            if (mCDTTextView != null) {
                mCDTTextView.setTimerCancle();
                mCDTTextView.setText("FAIL");
            }
            if (jCVideoPlayer != null){
                jCVideoPlayer.startButton.setEnabled(false);
            }
            JCVideoPlayer.releaseAllVideos();
        }
    }

}
